package com.aismy.admin.controller;

import com.aismy.admin.dto.ArticleDTO;
import com.aismy.admin.service.ArticleService;
import com.aismy.admin.utils.SecurityUtils;
import com.aismy.admin.vo.ArticleVO;
import com.aismy.admin.vo.Result;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping
    public Result<Long> createArticle(@Validated @RequestBody ArticleDTO articleDTO) {
        Long userId = SecurityUtils.getUserId();
        Long articleId = articleService.createArticle(articleDTO, userId);
        return Result.success(articleId);
    }

    @PutMapping
    public Result<Void> updateArticle(@Validated @RequestBody ArticleDTO articleDTO) {
        Long userId = SecurityUtils.getUserId();
        articleService.updateArticle(articleDTO, userId);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        articleService.deleteArticle(id, userId);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<ArticleVO> getArticleDetail(@PathVariable Long id) {
        ArticleVO article = articleService.getArticleDetail(id);
        return Result.success(article);
    }

    @GetMapping("/list")
    public Result<Page<ArticleVO>> getArticleList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<ArticleVO> articlePage = articleService.getArticleList(page, size);
        return Result.success(articlePage);
    }

    @GetMapping("/user/{userId}")
    public Result<Page<ArticleVO>> getUserArticles(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<ArticleVO> articlePage = articleService.getUserArticles(userId, page, size);
        return Result.success(articlePage);
    }


    @GetMapping("/latest")
    public Result<List<ArticleVO>> getLatestArticles() {
        return Result.success(articleService.getLatestArticles(2));
    }
} 